<template>
  <div class="login-form">
    <div class="g-form">
      <div class="g-form-line">
        <span class="g-form-label">用户名：</span>
        <div class="g-form-input">
          <input type="text" v-model="usernameModel"  placeholder="请输入用户名">
        </div>
        <span class="g-form-error" v-if="userErrors">{{userErrors.errorText }}</span>
      </div>
      <div class="g-form-line">
        <span class="g-form-label">密码：</span>
        <div class="g-form-input">
          <input type="password" v-model="passwordModel"  placeholder="请输入密码">
        </div>
        <span class="g-form-error" v-if="passwordErrors">{{passwordErrors.errorText}}</span>
      </div>
      <div class="g-form-line">
        <span class="g-form-label">确认密码：</span>
        <div class="g-form-input">
          <input type="password" v-model="passwordcheckModel"  placeholder="请再输入一次密码">
        </div>
        <span class="g-form-error" @click="passwordcheckErrors">{{passwordcheckErrors.errorText}}</span>
      </div>
      <div class="g-form-line">
        <div class="g-form-btn">
          <a class="button" @click="onLogin" >登录</a>
        </div>
      </div>
      <p>{{ errorText }}</p>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: 'HeaderLogForm',
  data () {
    return {
      usernameModel: '',
      passwordModel: '',
      passwordcheckModel: '',
      errorText: '',
      userFlag: false,
      passwordFlag: false,
      passwordcheckFlag: false
    }
  },
  computed: {
    userErrors () {
      let errorText, status
      if (!/@/g.test(this.usernameModel)) {
        status = false
        errorText = '账号必须包含@'
      } else {
        status = true
        errorText = ''
      }
      if (!this.userFlag) { // 意思就是页面没跑之前 进入这里，返回的是 ''和true的形式
        errorText = ''
        this.userFlag = true // 一次性再也进不来了，除非刷新页面
      }
      return {
        status,
        errorText
      }
    },
    passwordErrors () {
      let errorText, status
      if (/^\w{0,5}$/g.test(this.passwordModel)) {
        status = false
        errorText = '密码不得小于6位'
      } else {
        status = true
        errorText = ''
      }
      if (!this.passwordFlag) {
        errorText = ''
        this.passwordFlag = true
      }
      return {
        status,
        errorText
      }
    },
    passwordcheckErrors () {
      let errorText, status
      // if (!/^\w{0,5}$/g.test(this.passwordcheckModel)) errorText = '密码不得少于6位 } else
      if (this.passwordcheckModel !== this.passwordModel) {
        errorText = '两次密码不匹配'
      } else {
        status = true
        errorText = ''
      }
      if (!this.passwordcheckFlag) {
        errorText = ''
        this.passwordcheckFlag = true
      }
      return {
        status,
        errorText
      }
    }
  },
  methods: {
    onLogin () {
      if (!this.userErrors.status || !this.passwordErrors.status) {
        this.errorText = '部分选项未通过'
      } else {
        this.errorText = ''
        alert('登陆成功')
        axios.get('/app/login')
          .then((res) => {
            this.$emit('has-log', res.data)
          }, (error) => {
            console.log(error)
          })
      }
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="stylus">
  .g-form-input
    border 1px solid #ccc
    height 30px
    width 200px
    line-height 30px
    vertical-align middle
    padding 0 10px
    display inline-block
  .g-form-line
    padding 15px 0 0
  .g-form-label
    width 100px
    font-size 16px
    display inline-block
  .g-form-btn
    margin 20px 0 0 130px
    background #40AF7F
    width 60px;
    line-height 35px
    text-align center
    cursor: pointer;
    a
      color white!important
  .g-form-error
    color red
    padding-left 10px
</style>
